import "dart:developer";

import "package:sports_new/entity/pay/usable_coupon.dart";
import "package:sports_new/server/http/dio_utils.dart";
import "package:sports_new/utils/user.dart";
import "package:sports_new/widgets/pay_bottom_sheet.dart";

import "../../entity/pay/coupon_pack_entity.dart";
import "../api_dir.dart";

class CouponApi {
  // //status
  // //0 表示 已过期 或 已使用 的券
  // //1 表示 可使用 或 待生效 的券
  // Future<List<MyCouponEntity>?> myCoupons({int status = 1}) async {
  //   try {
  //     final response = await DioUtils.post(
  //       "/${ApiDir.user}/usr-coupon-do/myCouponList",
  //       params: {
  //         "status":status,
  //         "userId":User.auth?.userId
  //       });
  //     return (response.data["d"] as List).map((e) => MyCouponEntity.fromJson(e)).toList();
  //   } catch (err) {}
  //   return null;
  // }

  //gold 优惠前价格
  //scene 1 购买方案
  //otherId 购买方案传专家id
  static Future<List<UsableCouponEntity>?> use1Coupons(
      {required double gold, String? otherId, required PayScene scene,int? roomId}
    ) async {
    // if(roomId != null) return [];
    try {
      final response =
          await DioUtils.post("/${ApiDir.user}/usr-coupon-do/canUseCouponList", params: {
        "gold": gold,
        "otherId": otherId,
        "scene": scene.type,
        "userId": User.auth?.userId
      });
      return (response.data["d"] as List)
          .map((e) => UsableCouponEntity.fromJson(e))
          .toList();
    } catch (err) {
      log("use1 coupon err $err");
    }
    return null;
  }

  ///优惠券包
  static Future<List<CouponPackEntity>> getCouponPack() async {
    final result = await DioUtils.post("/${ApiDir.activity}/app-act-goods-group-do/search");
    try {
      if (result.statusCode == 200 && result.data['c'] == 200) {
        List<CouponPackEntity> data = result.data['d']?.map<
          CouponPackEntity>(
            (e) => CouponPackEntity.fromJson(e)
        ).toList() ?? [];
        return data;
      }
    }catch(e){
      log("$e");
      return [];
    }
    return [];
  }

  ///优惠券包单个
  static Future<CouponPackEntity> getCouponPackSingle(int packId) async {
    final result = await DioUtils.post("/${ApiDir.activity}/app-act-goods-group-do/queryByPackageId",params: packId);
    try {
      if (result.statusCode == 200 && result.data['c'] == 200) {
        CouponPackEntity data = CouponPackEntity.fromJson(result.data['d']);
        return data;
      }
    }catch(e){
      log("$e");
      return CouponPackEntity();
    }
    return CouponPackEntity();
  }
}
